﻿using UnityEngine;
using System;
using LitJson;

namespace Shipwar.SDK
{
    public class JJSDK : SDK
    {
        private AndroidJavaObject CurrentActivity;
        private AndroidJavaClass JJSDKClass;
        private AndroidJavaProxy SDKCallback;

        public enum ERRCODE
        {
            TK_RESULT_SUCCESS = 0,
            TK_RESULT_LOGIN_PWD_ERROR = 2001,
            TK_RESULT_LOGIN_USER_FORBIDDEN = 2002,
            TK_RESULT_LOGIN_SMS_CODE_ERROR = 2003,
            TK_RESULT_LOGIN_VERIFYCODE_INVALID = 2004,
            TK_RESULT_LOGIN_LOGINNAME_ILLEGAL = 2005,
            TK_RESULT_LOGIN_NEED_SMS_CONFIRM = 2006,
            TK_RESULT_LOGIN_PWD_ILLEGAL = 2007,
            TK_RESULT_LOGIN_LOGINNAME_NOT_EXIST = 2008,
            TK_RESULT_LOGIN_TECENT_OAUTH_FAILED = 2009,
        }

        public JJSDK()
        {
            AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            CurrentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
            JJSDKClass = new AndroidJavaClass("com.jjworld.shipwar.jjSdk");
            SDKCallback = new JJSDKCallback(this);
            AndroidJavaObject tkam = JJSDKClass.GetStatic<AndroidJavaObject>("tkam");
            tkam.Call("setTKAPICallback", SDKCallback);
        }

        public override void StartLogin()
        {
            JJSDKClass.CallStatic("autoLogin");
        }

        public override bool HasSDK()
        {
            return true;
        }

        public override void OpenShop()
        {
            JJSDKClass.CallStatic("getGoodsList");
        }

        public override void BeginBuy(string sku)
        {
            JJSDKClass.CallStatic("selectPayMethod", sku);
        }

        public class JJSDKCallback: AndroidJavaProxy
        {
            SDK _sdk;

            public JJSDKCallback(SDK sdk) : base("cn.jj.jjgamesdk.ITKAPICallback")
            {
                _sdk = sdk;
            }

            void onLoginFinished(int retCode, string result)
            {
                ERRCODE status = (ERRCODE)retCode;
                if (status != ERRCODE.TK_RESULT_SUCCESS)
                    return;

                JsonData json = JsonMapper.ToObject(result);
                User newUser = new User();
                newUser.AppUID = ((int)json["userId"]).ToString();
                newUser.APPToken = (string)json["oidReqToken"];
                newUser.LoginType = (int)json["accountType"];
                newUser.Name = (string)json["nickname"];
                _sdk.currentUser = newUser;
                _sdk.FireOnLogin();
            }

            void onAuthUserFinished(int retCode, string result)
            {
                ERRCODE status = (ERRCODE)retCode;
                if (status != ERRCODE.TK_RESULT_SUCCESS)
                    return;

                JsonData json = JsonMapper.ToObject(result);
                User newUser = new User();
                newUser.AppUID = ((int)json["userId"]).ToString();
                newUser.APPToken = (string)json["oidReqToken"];
                newUser.LoginType = (int)json["accountType"];
                newUser.Name = (string)json["nickname"];
                _sdk.FireOnLogin();
            }

            void onSelectPayMethodFinished(int retCode, string result)
            {
                
            }

            void onPayOrderFinished(int retCode, string result)
            {
                
            }

            void onGetGoodsListFinished(int retCode, string result)
            {
                if ((ERRCODE)retCode != ERRCODE.TK_RESULT_SUCCESS)
                    return;
                if (_sdk.OpenShopCallback != null)
                    _sdk.OpenShopCallback.Invoke();
            }
        }
    }
}
